package de.sono.radio.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.RemoteViews;
import de.sono.radio.R;
import de.sono.radio.list.RadioListAdresses;

public class RadioStreamingService extends Service {

	public static String STREAM_URL = "meadia_stream_url";
	public static String STREAM_ICON = "media_stream_icon";
	public static String STREAM_TITLE = "media_stream_title";

	public static String STREAM_START = "media_stream_start";
	public static String STREAM_STOP = "media_stream_stop";

	public static int STREAM_NOTIFICATION_ID = 140484;

	private MediaPlayer mStreamPlayer;

	@Override
	public void onCreate() {
		super.onCreate();

		mStreamPlayer = new MediaPlayer();
		mStreamPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mStreamPlayer.release();
		mStreamPlayer = null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		if (intent.getBooleanExtra(STREAM_START, false)) {
			startStream(intent);
		} else if (intent.getBooleanExtra(STREAM_STOP, false)) {
			stopStream(intent);
		} else {
			// TODO
		}

		return super.onStartCommand(intent, flags, startId);
	}

	private void startStream(Intent intent) {
		String url = intent.getStringExtra(STREAM_URL);
		String title = intent.getStringExtra(STREAM_TITLE);
		byte[] raw_icon = intent.getByteArrayExtra(STREAM_ICON);

		Bitmap icon = BitmapFactory.decodeByteArray(raw_icon, 0, raw_icon.length);
		
		PendingIntent pi = PendingIntent
				.getService(getApplicationContext(), 0, new Intent(
						getApplicationContext(), RadioStreamingService.class),
				PendingIntent.FLAG_UPDATE_CURRENT);
		Notification notification = new Notification();

		RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
		contentView.setImageViewBitmap(R.id.notification_image, icon);
		contentView.setTextViewText(R.id.notification_text, title);
		notification.contentView = contentView;
		notification.flags |= Notification.FLAG_ONGOING_EVENT;
		notification.contentIntent = pi;
		
		NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		manager.notify(STREAM_NOTIFICATION_ID, notification);

		try {

			mStreamPlayer.setDataSource(RadioListAdresses.ARD_BASE + url);
			mStreamPlayer.prepare();
			mStreamPlayer.start();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			manager.cancel(STREAM_NOTIFICATION_ID);
		}

	}

	private void stopStream(Intent intent) {
		
		NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		manager.cancel(STREAM_NOTIFICATION_ID);

		mStreamPlayer.stop();
		
		stopSelf();

	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
